package y2017.date0226pm.颜色表注释调整;

import java.io.*;

public class WriteServer {
    private WriteServer() {
    }

    public static WriteServer getServer() {
        return new WriteServer();
    }

    public void writeStringToFile(String path, String content) {
        File file = new File(path);

        if (file.exists()) {
            System.out.println("文件已存在，写入副本");
            int lastDotIndex = path.lastIndexOf(".");
            String lastName = "";
            if (lastDotIndex == -1) {
                lastDotIndex = path.length();
            } else {
                lastName = path.substring(lastDotIndex + 1, path.length());
            }
            path = path.substring(0, lastDotIndex) + "_副本." + lastName;
            file = new File(path);
        }
        try {
            FileOutputStream fos = new FileOutputStream(file);
            OutputStreamWriter osw = new OutputStreamWriter(fos);
            BufferedWriter bw = new BufferedWriter(osw);
            bw.write(content);
            bw.flush();
            bw.close();
            osw.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

}
